home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / New System Software Extensions / QuickDraw™ GX v1.0ß2 / Sample Code / Printing Samples / Extensions… / Extension Shell ƒ / Extension.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-08  |  8.8 KB  |  322 lines  |  [TEXT/MPS ]

  1. /*________________________________________________________
  2.  
  3.     File: Extension.c
  4.  
  5.     C code for a printing extension.
  6.  
  7.     Dave Hersey
  8.     Apple Developer Technical Support
  9.  
  10.     12/01/92 - dmh - Created.
  11.      4/26/93 - dmh - Updated to use recommended approach
  12.                       to global data initialization.
  13.      9/05/93 - dmh - Updated to b2.
  14.                     - Fixed minor problem with highlighting
  15.                      of editText panel items.
  16.                    - Switched to Exception.h assertion stuff
  17.                      for error checking.
  18.  
  19.     (Note: all functions are in the Mark menu.)
  20.     
  21. __________________________________________________________*/
  22.  
  23. #include "Extension.h"
  24.  
  25.  
  26. /*******************************************************************
  27.     InitGlobalData is used to initialize any global data we need to
  28.     in our initialize message override.  It's critical that you do
  29.     things this way, rather than access the data in the same scope
  30.     that you call NewMessageGlobals.  Otherwise, some compilers
  31.     will give you code that references an invalid A5 world.
  32.  
  33. ********************************************************************/
  34.  
  35. OSErr InitGlobalData()
  36. {
  37. // Initialize any global data here.
  38.     
  39.     return noErr;
  40. }
  41.  
  42.  
  43. /*******************************************************************
  44.     NewInitialize is our override for the GXInitialize message.  In
  45.     here, you shouldn't initialize anything directly-- call
  46.     InitGlobalData for that.  Once you create the A5 world with
  47.     NewMessageGlobals, you can access your global data just like
  48.     you were an application.  Whenever you're called, your global
  49.     data will be valid.
  50.     
  51. ********************************************************************/
  52.  
  53. OSErr NewInitialize()
  54. {
  55.     OSErr    err;
  56.  
  57. // Create an A5 world, and initialize our global data.
  58.  
  59.     err = NewMessageGlobals(A5Size(), A5Init);
  60.     
  61.     if (!err) err = InitGlobalData();
  62.     
  63.     return err;
  64. }
  65.  
  66.  
  67. /*******************************************************************
  68.     NewShutDown is our override for the GXShutDown message.  We
  69.     simply throw away our A5 world which we created in our
  70.     GXInitialize message override, NewInitialize.
  71.     
  72. ********************************************************************/
  73.  
  74. OSErr NewShutDown()
  75. {
  76.     DisposeMessageGlobals();
  77.     return noErr;
  78. }
  79.  
  80.  
  81. /*******************************************************************
  82.     NewSpoolPage is our override for the GXSpoolPage message.  We
  83.     check to see if we're enabled, and if so, give a SysBeep before
  84.     forwarding.
  85.     
  86. ********************************************************************/
  87.  
  88. OSErr NewSpoolPage(gxSpoolFile spFile, gxFormat aFormat, gxShape pgShape)
  89. {
  90.     OSErr                    err;
  91.     ExtensionCollection        extCollect;
  92.     
  93. // Try to retrieve our collection item.  If we can't find it, we'll
  94. // act as if the user had us turned off or on (as determined by
  95. // kDefaultSetting).
  96.  
  97.     err = GetJobCollectionItem(&extCollect, nil, kExtensionCollectionType,
  98.                                gxPrintingTagID);
  99.  
  100.     if (err)
  101.     {
  102.         extCollect.extTurnedOn = kDefaultSetting;
  103.         err = noErr;
  104.     }
  105.  
  106.  
  107. // If we're turned on, beep so we know we've been here, then forward
  108. // this message down the chain.
  109.  
  110.     if (extCollect.extTurnedOn)
  111.         SysBeep(3);
  112.     
  113.     err = Forward_GXSpoolPage(spFile, aFormat, pgShape);
  114.  
  115.     return err;
  116. }
  117.  
  118.  
  119. /*******************************************************************
  120.     NewJobPrintDialog is our override for GXJobPrintDialog.  All we
  121.     do is set up our panel and then forward the message.
  122.     
  123. ********************************************************************/
  124.  
  125. OSErr NewJobPrintDialog(gxDialogResult *dlogResult)
  126. {
  127.     SetUpPrintPanel();
  128.     return Forward_GXJobPrintDialog(dlogResult);
  129. }
  130.  
  131.  
  132. /*******************************************************************
  133.     NewHandlePanelEvent is our override for GXHandlePanelEvent. If
  134.     the event is one of ours, we handle it.  Otherwise, we just
  135.     forward it down the chain.
  136.     
  137. ********************************************************************/
  138.  
  139. OSErr NewHandlePanelEvent(gxPanelInfoRecord *panelInfo)
  140. {
  141.     OSErr            err = noErr;
  142.     GrafPtr            oldPort;
  143.     DialogPtr        pDlg;
  144.  
  145. // Get a pointer to the dialog, save our current grafPort,
  146. // and set us to the dialog's port.
  147.  
  148.     pDlg = panelInfo->pDlg;
  149.     GetPort(&oldPort);
  150.     SetPort(pDlg);
  151.  
  152.     switch (panelInfo->panelEvt)    // Handle any of these events as need be.
  153.     {
  154.         case gxPanelOpenEvt:                // Initialize and draw.
  155.             break;
  156.         case gxPanelCloseEvt:                // Your panel is going away (panel switch,
  157.             break;                            // confirm or cancel).
  158.  
  159.         case gxPanelHitEvt:                    // There's a hit in your panel.
  160.             break;
  161.         case gxPanelFilterEvt:                // This is called to filter every event.
  162.             break;
  163.         case gxPanelCancelEvt:                // The user has cancelled the dialog.
  164.             break;
  165.         case gxPanelConfirmEvt:                // The user has confirmed the dialog.
  166.             break;
  167.         case gxPanelUserWillConfirmEvt:        // user has selected confirm, time to
  168.             break;                            // parse your panel interdependencies.
  169.                                         
  170.         case gxPanelDialogEvt:                // Event to be handled by dialoghandler
  171.             break;                            // (you get to see all events).
  172.  
  173.         case gxPanelOtherEvt:                // osEvts, etc.
  174.             break;
  175.  
  176. // If our panel is activating/deactivating or if the focus (which
  177. // section of the dialog is active) has changed we need to activate
  178. // our editText fields appropriately.
  179.  
  180.         case gxPanelActivateEvt:            // The dialog window has just become active.
  181.         case gxPanelDeactivateEvt:            // The dialog window is becoming inactive.
  182.         case gxPanelIconFocusEvt:            // The user is moving to the icon list.
  183.         case gxPanelPanelFocusEvt:            // The user is moving to the panel.
  184.  
  185. /*  Here's what you would do if you had an editText field at DITL item #5:
  186.  
  187. #define d_edText    5
  188.  
  189.             if ((((DialogPeek) pDlg)->editField +1) == (panelInfo->itemCount +d_edText))
  190.             {
  191.                 if (panelInfo->panelEvt == gxPanelPanelFocusEvt)
  192.                     TEActivate(((DialogPeek) pDlg)->textH);
  193.                 else
  194.                     TEDeactivate(((DialogPeek) pDlg)->textH);
  195.             }
  196. */
  197.             break;
  198.     }
  199.  
  200. // Restore the original port as we leave.
  201.  
  202.     SetPort(oldPort);
  203.     return err;
  204. }
  205.  
  206.  
  207. /*******************************************************************
  208.     SetUpPrintPanel sets up our print panel, adding a default
  209.     ExtensionCollection item to the job collection.  This
  210.     collection item has the values we'll use to set up our panel's
  211.     controls.
  212.     
  213. ********************************************************************/
  214.  
  215. OSErr SetUpPrintPanel()
  216. {
  217.     OSErr                    err;
  218.     gxPanelSetupRecord        panelSetupRec;
  219.     ExtensionCollection        extConfig;
  220.  
  221. // Try to find our collection item.
  222.  
  223.     err = GetCollectionItem(GXGetJobCollection(GXGetJob()),
  224.                             kExtensionCollectionType,
  225.                             gxPrintingTagID,
  226.                             nil,
  227.                             &extConfig);
  228.  
  229.  
  230. // If we don't have an item in the job collection yet, store our default
  231. // settings in it.
  232.  
  233.     if (err == collectionItemNotFoundErr)
  234.     {
  235.         extConfig.extTurnedOn = kDefaultSetting;
  236.     
  237.         err = StoreJobCollectionItem(&extConfig, sizeof(ExtensionCollection),
  238.                                      kExtensionCollectionType, gxPrintingTagID,
  239.                                      true);
  240.  
  241.         nrequire(err, HaveCollectionMgrError);
  242.     }
  243.  
  244.  
  245. // Now, set up the panel.
  246.  
  247.     panelSetupRec.panelResId        = r_ExtensionPanel;        // which panel resource?
  248.     panelSetupRec.resourceRefNum    = GXGetMessageHandlerResFile(); // where is it?
  249.     panelSetupRec.refCon            = 0;                       // we don't use this.
  250.     panelSetupRec.panelKind            = gxExtensionPanel;     // This is an extension panel.
  251.  
  252.     err = GXSetupDialogPanel(&panelSetupRec);
  253.  
  254.  
  255. HaveCollectionMgrError:
  256.     
  257.     return err;
  258. }
  259.  
  260.  
  261. /*******************************************************************
  262.     StoreJobCollectionItem is a generic routine that stores a
  263.     collection item in the job collection.  If newItem is true,
  264.     a new collection item is added.  If it is false, an existing
  265.     one is replaced.
  266.     
  267. ********************************************************************/
  268.  
  269. OSErr StoreJobCollectionItem(void *collectItem, long collectSize, OSType collectType,
  270.                              short collectID, Boolean newItem)
  271. {
  272.     OSErr        err;
  273.     Collection    jobCollection;
  274.     long        index, itemSize, attributes;
  275.  
  276. // Get the job collection.  If we're adding a new collection item, do so.
  277. // Otherwise, get the existing item's index and replace the old collection
  278. // item.
  279.  
  280.     jobCollection = GXGetJobCollection(GXGetJob());
  281.  
  282.     if (newItem)
  283.         err = AddCollectionItem(jobCollection,
  284.                                 collectType,
  285.                                 collectID,
  286.                                 collectSize,
  287.                                 collectItem);
  288.     else
  289.     {
  290.         err = GetCollectionItemInfo(jobCollection,
  291.                                     collectType,
  292.                                     collectID,
  293.                                     &index,
  294.                                     &itemSize,
  295.                                     &attributes);
  296.         if (!err)
  297.             err = ReplaceIndexedCollectionItem(jobCollection,
  298.                                                index,
  299.                                                collectSize,
  300.                                                collectItem);
  301.     }
  302.  
  303.     return err;
  304. }
  305.  
  306.  
  307. /*******************************************************************
  308.     GetJobCollectionItem is a generic routine that retrieves a
  309.     collection item from the job collection.
  310.     
  311. ********************************************************************/
  312.  
  313. OSErr GetJobCollectionItem(void *collectItem, long *collectSize,
  314.                            OSType collectType, short collectID)
  315. {
  316.     return GetCollectionItem(GXGetJobCollection(GXGetJob()),
  317.                              collectType,
  318.                              collectID,
  319.                              collectSize,
  320.                              collectItem);
  321. }
  322.